//
//  ShopMenu.m
//  AirPort
//
//  Created by henrySun on 12-9-28.
//  Copyright (c) 2012年 广州民航信息技术有限公司. All rights reserved.
//



#import "Utility.h"
#import "PersonalMenu.h"
#import "ArrowView.h"
#import "Config.h"

@implementation PersonalMenu

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self shopMenu];
    }
    return self;
}
-(void) shopMenu{
    self.itemArray=[NSMutableArray array];
    UIView *newsMenu=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 90)];
 
    ArrowView *leftButton=[[ArrowView alloc] initWithFrame:CGRectMake(0, 24,21,42)];
    leftButton.image=[Utility imageNamed:@"qz_scroll_arrow_left.png"];
    leftButton.tag=HOMEPAGESECONDLEFTBUTTONTAG;
    leftButton.hidden=YES;
    leftButton.delegate=self;
    [newsMenu  addSubview:leftButton];
	leftButton.direction = kLeft;
    ArrowView *rightButton=[[ArrowView alloc] initWithFrame:CGRectMake(299, 24,21,42)];
    rightButton.image=[Utility imageNamed:@"qz_scroll_arrow_right.png"];
    rightButton.tag=HOMEPAGESECONDRIGHTBUTTONTAG;
    rightButton.delegate=self;
    rightButton.hidden=YES;
    rightButton.direction = kRight;
    newsMenu.backgroundColor=[UIColor redColor];
    [newsMenu  addSubview:rightButton];
    
    
    newsMenu.backgroundColor=[UIColor  clearColor];
    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(30, 0, 260, 90)];
    self.menuScrollView=scrollView;
    self.menuScrollView.delegate=self;
    self.menuScrollView.showsHorizontalScrollIndicator=NO;
    [self.menuScrollView setBackgroundColor:[UIColor  clearColor]];
    [newsMenu addSubview:self.menuScrollView];
    [self addSubview:newsMenu];

    
}
-(void) showData:(NSArray *) param{
    [self.itemArray removeAllObjects];
    [self.itemArray addObjectsFromArray:param];
    for (UIView  *view in self.menuScrollView.subviews){
        [view removeFromSuperview];
    }
    if (param&&[param  count]>0) {
        for (UIView  *view  in self.menuScrollView.subviews) {
            for (UIView  *subview  in view.subviews) {
                [subview removeFromSuperview];
            }
            [view removeFromSuperview];
        }
        
     	[self.menuScrollView setCanCancelContentTouches:NO];
        //[menuScrollView setClipsToBounds:YES];
        self.menuScrollView.scrollEnabled=YES;
        // menuScrollView.pagingEnabled = YES;
        self.menuScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        float xPosition = 0;
        //第一按钮 默认显示
        for ( int i=0;i<self.itemArray.count;i++) {
            NSDictionary *dict=[self.itemArray objectAtIndex:i];
            CGRect frame=CGRectMake(xPosition, 21, 80, 52);
            UIButton *button=[Utility customButton:self action:@selector(thumbButtonAction:) normalImage:[NSString stringWithFormat:@"mp_%@button_normal.png",[dict objectForKey:@"name"]]lightedImage:[NSString stringWithFormat:@"mp_%@button_normal.png",[dict objectForKey:@"name"]] frame:frame title:nil tag:HOMEPAGESECONDCENTERBUTTONTAG+i];
            if ([[dict objectForKey:@"count"] integerValue]>0) {
                UILabel *label=[Utility detailTextLabel:CGRectMake(0, 30, frame.size.width, 15) andFontSize:12.0];
                label.text=[dict objectForKey:@"count"];
                label.textAlignment=NSTextAlignmentCenter;
                label.textColor=UIColorFromRGB(0x000000);
                [button addSubview:label];
            }
            
            [self.menuScrollView addSubview:button];
            xPosition += frame.size.width + 12;
        }
        [self.menuScrollView setContentSize:CGSizeMake(xPosition, self.menuScrollView.frame.size.height)];
        if (self.menuScrollView.frame.size.width<self.menuScrollView.contentSize.width) {
            ArrowView *rightButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
            rightButton.hidden=NO;
        }
    }
}

-(IBAction)thumbButtonAction:(id)sender{
    UIButton *button=(UIButton*)sender;
    if ([self.delegate respondsToSelector:@selector(onclickScrollButton:)]) {
        [self.delegate  onclickScrollButton:button.tag-HOMEPAGESECONDCENTERBUTTONTAG];
    }
}

#pragma mark ArrowViewDelegate
- (BOOL) scroll:(ArrowView *)arrowView withDirection:(NSInteger)direction {
    ArrowView *leftButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDLEFTBUTTONTAG];
    ArrowView *rightButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
	CGPoint offset = self.menuScrollView.contentOffset;
	float maxX = self.menuScrollView.contentSize.width - self.menuScrollView.bounds.size.width;
	if (direction == kLeft) {
		offset.x -= 4;
	}
	else {
		offset.x += 4;
	}
	if (offset.x < 0) {
		offset.x = 0;
	}
	else if (offset.x > maxX) {
		offset.x = maxX;
	}
	self.menuScrollView.contentOffset = offset;
	if (offset.x == 0 || offset.x == maxX) {
		return YES;
	}
	else {
        leftButton.hidden = NO;
        rightButton.hidden = NO;
		return NO;
	}
}

#pragma UIscrollDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    ArrowView *leftButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDLEFTBUTTONTAG];
    leftButton.hidden=NO;
    ArrowView *rghtButton=(ArrowView *)[self  viewWithTag:HOMEPAGESECONDRIGHTBUTTONTAG];
    rghtButton.hidden=NO;
    
    if (self.menuScrollView.contentOffset.x<1) {
        leftButton.hidden=YES;
    }
    if (self.menuScrollView.contentOffset.x>self.menuScrollView.contentSize.width-self.menuScrollView.frame.size.width-1) {
        rghtButton.hidden=YES;
    }
    
}
-(void) dealloc{
     _delegate=nil;
    self.menuScrollView.delegate=nil;
    }

@end
